package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程+课程简介合并   前端控制器
 * </p>
 *
 * @author hwt
 * @since 2022-08-23
 */
@Api(tags = {"课程管理"})
@RestController
@RequestMapping("eduservice/course")
public class EduCourseController {

    @Resource
    EduCourseService eduCourseService;

    // 添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourse(@RequestBody CourseInfoVo infoVo) {

        return eduCourseService.savaCourseInfo(infoVo);

    }

    // 根据课程 id 查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable("courseId") String courseId) {

        return eduCourseService.getCourseInfo(courseId);
    }

    // 修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {

        return eduCourseService.updateCourseInfo(courseInfoVo);

    }

    // 根据课程 id 获取到课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        return eduCourseService.getPublishCourseInfo(id);
    }

    // 根据课程 id 获取到课程确认信息
    @GetMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        return eduCourseService.PublishCourse(id);
    }


    @ApiOperation("课程分页查询")
    @PostMapping("pageCourse/{current}/{limit}")
    public R pageListTeacher(@PathVariable("current") long current,
                             @PathVariable("limit") long limit,
                             @RequestBody(required = false) CourseQuery query) {

        // 创建分页对象 current 当前页    limit 每页条数
        Page<CoursePublishVo> page = new Page<>(current, limit);

        // 进行分页
        page.setRecords(eduCourseService.selectCoursePage(query, page));

        // 总记录数
        long total = page.getTotal();

        // 当前页的数据
        List<CoursePublishVo> records = page.getRecords();


        return R.ok().data("total", total).data("rows", records);
    }


    @ApiOperation("删除课程")
    @PostMapping("deleteCourse")
    public R pageListTeacher(@RequestParam("id") String courseId) {

        // 前面使用的是这个删除方法，他里面是逻辑删除
        // return eduCourseService.deleteCourse(id);

        return eduCourseService.removeCurse(courseId);

    }

}

